<!-- 数据操作组件 -->
<template>
  <a-modal
    :title="title"
    :visible="visible"
    :confirm-loading="confirmLoading"
    @ok="handleOk"
    @cancel="close"
    :width="666"
    :destroyOnClose="true"
  >
    <a-form-model
      ref="ruleForm"
      :model="model"
      :rules="rules"
      :label-col="labelCol"
      :wrapper-col="wrapperCol"
    >
      <a-form-model-item label="密码" ref="newPassword" prop="newPassword">
        <a-input-password
          placeholder="请输入密码"
          allowClear
          v-model="model.newPassword"
        />
      </a-form-model-item>
    </a-form-model>
  </a-modal>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import { model, rules } from "./reset";
import { cleanObject } from "@/utils/CommonUtil";
import * as AesUtil from "@/utils/AesUtil";
export default {
  // eslint-disable-next-line vue/multi-word-component-names
  name: "Modal",
  // 子组件参数列表
  props: {},
  //import引入的组件需要注入到对象中才能使用
  components: {},
  data() {
    //这里存放数据
    return {
      api: {
        detail: "/admin_server/xx/", // 详情
        save: "/admin_server/auth/user/adminResetPassWord", // 新增
        update: "/admin_server/xx/update", // 修改
      },
      title: "",
      visible: false, // 数据操作组件是否打开
      disabled: false, // 是否禁用更改数据
      confirmLoading: false, // 提交按钮loading
      model, // 数据传输模型
      rules, // 数据输入校验
      labelCol: { span: 4 }, // label占位尺寸
      wrapperCol: { span: 18 }, // 输入框占位尺寸
    };
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    async handleOk() {
      this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          this.confirmLoading = true;
          const { success, result } = await this.$commonApi.rsaPublicKey();
          if (!success) {
            return;
          }
          // AES密钥
          let aeskey = AesUtil.createAesKey();
          const newPassword = this.model.newPassword;
          this.model.newPassword = AesUtil.AESEncrypt(
            this.model.newPassword,
            aeskey
          );
          this.model.encryptKey = AesUtil.RSAEncrypt(aeskey, result);
          const res = await this.$request.postRequest2(
            this.api.save,
            this.model
          );
          if (res.success) {
            this.$message.success("重置成功");
            this.$emit("handleOk");
          } else {
            this.model.newPassword = newPassword;
          }
          this.confirmLoading = false;
        }
      });
    },
    resetForm() {
      cleanObject(this.model);
      this.model.useStatus = 0;
    },
    async init(record) {
      this.title = record.username + "-重置密码";
      this.visible = true;
      this.$nextTick(() => {
        this.$refs.ruleForm.resetFields();
      });
      this.resetForm();
      this.model.userId = record.id;
      this.disabled = false;
    },
    close() {
      this.visible = false;
    },
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {},
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
};
</script>
<style>
</style>